#!/usr/bin/env node
// 为还没有的包创建 package.ejs、README.md 与 .npmignore

import fs from "fs";
import path from "path";

import {pkg as config} from "./utils/package.mjs";

import createPackageTemplate from "./createPackageTemplate.mjs";

const packagesDir = path.join(path.resolve(), "/packages/@azm");
const files = fs.readdirSync(packagesDir);

files.forEach((pkg) => {
  if (pkg.charAt(0) === ".") return;
  // 是插件
  const isPlugin = /^cli-plugin-/.test(pkg);
  const desc = isPlugin
    ? `${pkg.replace("cli-plugin-", "")} plugin for azm-cli`
    : `${pkg.replace("cli-", "")} for azm-cli`;

  const pkgPath = path.join(packagesDir, pkg, `package.json`);
  if (!fs.existsSync(pkgPath)) {

    fs.writeFileSync(
      pkgPath,
      createPackageTemplate(Object.assign(config, {name: pkg}))
    );
  }

  const readmePath = path.join(packagesDir, pkg, `README.md`);
  if (!fs.existsSync(readmePath)) {
    fs.writeFileSync(readmePath, `# @azm/${pkg}\n\n> ${desc}`);
  }

  const npmIgnorePath = path.join(packagesDir, pkg, `.npmignore`);
  if (!fs.existsSync(npmIgnorePath)) {
    fs.writeFileSync(npmIgnorePath, `__tests__\n__mocks__`);
  }
});
